/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.star.framework.excel.core;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.star.framework.excel.convert.CellValueConverter;
import cn.star.framework.excel.convert.CellValueValidateRule;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.converters.Converter;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import lombok.Getter;
import lombok.Setter;

/**
 * 列<br>
 *
 * @author zhaoweiping
 *     <p style='color: red'>Created on 2024-09-23 11:06:00
 * @since 3.0.0
 */
public class Column implements Serializable {

  /** 字段标识 */
  @Setter private String key;

  /** 字段名称 */
  @Setter private String[] name;

  /** 字段值 */
  @Getter @Setter private Object value;

  /** 是否忽略 */
  @Getter @Setter private boolean ignore = false;

  /**
   * 列顺序<br>
   * 支持多表头，是一个变化的值<br>
   * 切勿作为判断条件使用<br>
   */
  @Getter @Setter private Integer order = -1;

  /** 字段类型 */
  @Setter private Class<?> type;

  /** 原始字段信息 */
  @Getter @Setter private Field original;

  /** 值转换函数 */
  @Getter @Setter private CellValueConverter<?, ?> converter;

  /** 自定义校验规则 */
  private final List<CellValueValidateRule> cellValueValidateRules = new ArrayList<>();

  public Class<?> getType() {
    return type == null && original != null ? original.getType() : type;
  }

  public String getKey() {
    if (StrUtil.isBlank(key) && original != null) {
      return original.getName();
    }
    return key;
  }

  public String[] getName() {
    if (ArrayUtil.isEmpty(name)) {
      return new String[] {key};
    }

    return name;
  }

  public String getFirstName() {
    return name != null && name.length > 0 ? name[0] : null;
  }

  public Column addCellValueValidateRules(CellValueValidateRule... items) {
    if (ArrayUtil.isEmpty(items)) {
      this.addCellValueValidateRules(CollectionUtil.toList(items));
    }
    return this;
  }

  public Column addCellValueValidateRules(List<CellValueValidateRule> list) {
    cellValueValidateRules.addAll(list);

    return this;
  }

  public List<CellValueValidateRule> getCellValueValidateRules() {
    return CollectionUtil.sort(
        cellValueValidateRules, Comparator.comparingInt(CellValueValidateRule::getOrder));
  }

  @SuppressWarnings("all")
  public List<Converter<?>> getConverters() {
    List<Converter<?>> list = new ArrayList<>();
    try {
      list.add(ReflectUtil.newInstance(original.getAnnotation(ExcelProperty.class).converter()));
    } catch (Exception e) {
      // 忽略异常
    }

    return list;
  }
}
